#include "../include/muduo/proto/dispatcher.h"
#include "../include/muduo/proto/codec.h"
#include "../include/muduo/base/Logging.h"
#include "../include/muduo/base/Mutex.h"
#include "../include/muduo/net/EventLoop.h"
#include "../include/muduo/net/TcpClient.h"
#include "../include/muduo/net/EventLoopThread.h"
#include "../include/muduo/base/CountDownLatch.h"

#include "request.pb.h"
#include <iostream>

class Client
{
    public:
    typedef  std::shared_ptr<google::protobuf::Message> messagePtr;
    typedef std::shared_ptr<lrl::AddRespone> AddResponePtr;
    typedef std::shared_ptr<lrl::TranslateRespone> TranslateResponePtr;

    Client(const std::string& sip,int port)
    :_latch(1),_client(_loopthread.startLoop(),muduo::net::InetAddress(sip,port),"Cilent"),
    _diasptcher(std::bind(&Client::onUnknownMessage,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3)),
    _codec(std::bind(&ProtobufDispatcher::onProtobufMessage,&_diasptcher,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3))
    {
        _diasptcher.registerMessageCallback<lrl::TranslateRespone>(std::bind(&Client::onTranslate,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));
        
        _diasptcher.registerMessageCallback<lrl::AddRespone>(std::bind(&Client::onAdd,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));

        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage,&_codec,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));

        _client.setConnectionCallback(std::bind(&Client::onConnection,this,std::placeholders::_1));
    }
    void connect()
    {
        _client.connect();
        _latch.wait();
    }

    void Translate(const std::string& msg)
    {
        lrl::TranslateRequest resq;
        resq.set_msg(msg);
        send(&resq);
    }
    void Add(int num1,int num2)
    {
        lrl::AddRequest resq;
        resq.set_num1(num1);
        resq.set_num2(num2);
        send(&resq);
    }
    private: 

    bool send(const google::protobuf::Message *message) 
    {
        if(_conn->connected())
        {
            _codec.send(_conn,*message);
            return true;
        }
        return true;
    }   

    void onTranslate(muduo::net::TcpConnectionPtr& conn,const TranslateResponePtr& message,muduo::Timestamp)
    {
        std::cout << "翻译结果：" << message->msg() << std::endl; 
    }

    void onAdd(const muduo::net::TcpConnectionPtr& conn,const AddResponePtr& message, muduo::Timestamp)
    {
        std::cout << "加法结果:" <<message->result() <<std::endl; 
    }

    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn, const MessagePtr& message, muduo::Timestamp)
    {
        LOG_INFO <<"onUnknowmessage" <<message->GetTypeName();
        conn ->shutdown();
    }

    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn -> connected())
        {
            _latch.countDown();
            _conn = conn;
        }
        else
        {
            _conn.reset();
        }
    }
    private:
    

    muduo::CountDownLatch _latch;
    muduo::net::EventLoopThread _loopthread;
    muduo::net::TcpConnectionPtr _conn;
    muduo::net::TcpClient _client;
    ProtobufDispatcher _diasptcher;//请求分发器
    ProtobufCodec _codec;//协议处理器
};

int main()
{
    Client cil("127.0.0.1",8085);
    cil.connect();

    cil.Translate("hello");
    cil.Add(11,22);

    
    sleep(1);
    return 0;
}